/**
 * @Author: lclangcheng
 * @Date:   2018-10-08 16:01:35
 * @Email:  lclangcheng@gmail.com
 * @Filename: error.js
 * @Last modified by:   lclangcheng
 * @Last modified time: 2018-10-08 16:02:04
 * @License: jin10
 * @Copyright: jin10
 */
'use strict'

// const log = require('../libs/log')
const env = process.env.NODE_ENV

module.exports = async (ctx, next) => {
  try {
    await next()
  } catch (err) {
    if (err.status) {
      ctx.body = { status: err.status, message: err.message }
      if (env === 'production' && err.name === 'ValidationError') {
        // 处理校验参数报错逻辑
        ctx.body.message = '参数有误'
      }
    } else {
      ctx.status = 503
      ctx.body = err.stack
    }
  }
}
